package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.cargo.ContractDao;
import com.itheima.dao.cargo.ContractProductDao;
import com.itheima.dao.cargo.ExtCproductDao;
import com.itheima.doamin.cargo.Contract;
import com.itheima.doamin.cargo.ExtCproduct;
import com.itheima.doamin.cargo.ExtCproductExample;
import com.itheima.service.cargo.ExtCproductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 附件
 */
@Service
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    ExtCproductDao extCproductDao;
    @Autowired
    ContractProductDao contractProductDao;
    @Autowired
    ContractDao contractDao;

    /**
     * 保存
     *
     * @param extCproduct
     */
    @Override
    public void save(ExtCproduct extCproduct) {
        //添加,生成id
        extCproduct.setId(UtilFuns.getUUID());
        //判断用户输入的是否为空值
        double cAmount = 0d;
        if (UtilFuns.isNotEmpty(extCproduct.getCnumber()) && UtilFuns.isNotEmpty(extCproduct.getPrice())) {
            //计算
            cAmount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        //更新附件的总价值
        extCproduct.setAmount(cAmount);

        //计算合同的总价
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setExtNum(contract.getExtNum() + 1);
        contract.setTotalAmount(contract.getTotalAmount() + cAmount);

        //对附件进行插入
        extCproductDao.insertSelective(extCproduct);
        //对合同进行更新
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 更新
     *
     * @param extCproduct
     */
    @Override
    public void update(ExtCproduct extCproduct) {
        ExtCproduct oldExtCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        //对附件进行更新
        double cAmount = 0d;
        //判断用户输入的是否为空值
        if (UtilFuns.isNotEmpty(extCproduct.getPrice()) && UtilFuns.isNotEmpty(extCproduct.getCnumber())) {
            cAmount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        //附件更新前的总价
        double oldAmount = oldExtCproduct.getAmount();
        //设置更新后的总价
        extCproduct.setAmount(cAmount);


        //计算合同的总价
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() - oldAmount + cAmount);

        //对附件进行更新
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        //对合同进行更新
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        //根据id获得附件的总价
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        double amount = extCproduct.getAmount();

        //对合同总额进行减少
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() - amount);
        //附件数
        contract.setExtNum(contract.getExtNum() - 1);


        //对附件进行删除
        extCproductDao.deleteByPrimaryKey(id);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /**
     * 根据id查询
     *
     * @param id
     */
    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    /**
     * 分页查询
     *
     * @param example
     * @param page
     * @param size
     */
    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        //获得附件的pageInfo
        PageHelper.startPage(page, size);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);
        return new PageInfo(extCproducts);
    }
}
